﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Text.RegularExpressions;
using BAL.EstRec;
using System.Data;
using DAL.EstRec;

namespace Nav3.PasteExcelData
{
    public class ModelPasteExcelData
    {
        string clipboardData;
        List<Recurso> catalogRecursos;
        List<Pos> catalogPos;

        public ModelPasteExcelData(IDataObject data)
        {
            
        }
        public ModelPasteExcelData()
        {

        }

        public List<Recurso> IDrecurso(string vv)
        {

            DataTable dt = new DataTable();
            List<Recurso> aux = new List<Recurso>();

            dt = SrvDB.ExecuteQuery("EXEC SP_COPY_EXCEL_RECURSO_IDS @VERSION = " + vv);

            for (int i = 0; i < dt.Columns.Count; i++) {

                Recurso p = new Recurso(dt.Rows[i][0].ToString(), "");
                aux.Add(p);
            }

            return aux;
        }

        public bool validaRecPos2FDV(List<CapturaPivotFDV>  DatosIDS, string sPOS, string sRecursos){

            var list = DatosIDS;

            int count = (from hq in list
                         where hq.recurso.idRecurso == sRecursos && hq.pos.idPos == sPOS
                         select hq).Count();

            if (count > 0)
                return true;
            return false;
        }

        public bool validarRecurso(List<Recurso> RecExistentes, string recActual)
        {

            var list = RecExistentes;

            int count = (from hq in list
                         where hq.recurso == recActual
                         select hq).Count();

            if (count > 0)
                return true;
            return false;
        }

        public List<Pos> IDpos(string vv) {

            DataTable dt = new DataTable();
            List<Pos> aux = new List<Pos>();

            dt = SrvDB.ExecuteQuery("EXEC SP_COPY_EXCEL_POS_IDS @VERSION = " + vv);

            for (int i = 0; i < dt.Columns.Count; i++) {

                Pos p = new Pos(dt.Rows[i][0].ToString(), "");
                aux.Add(p);
            }

            return aux;
        }

        public bool validarPOS(List<Pos> PosExistentes, string posActual)
        {

            var list = PosExistentes;
            
            int count = (from hq in list
                         where hq.pos == posActual
                         select hq).Count();

            if (count > 0)
                return true;
            
            return false;
        }

        public void convertClipboardData(string clipboardData)
        {
            try
            {
                string[] lines = Regex.Split(clipboardData, "\r\n");
                var res = lines.Select(d =>
                {
                    CapturaPivotFDV cpf = null;
                    try
                    {
                        cpf = new CapturaPivotFDV(new Pos(d.Split('\t')[0].ToString().Trim(), d.Split('\t')[0].ToString().Trim())
                                                         , new Recurso(d.Split('\t')[1].ToString().Trim(), d.Split('\t')[1].ToString().Trim())
                                                         , Convert.ToInt32(d.Split('\t')[2].ToString()));
                    }
                    catch (Exception ex)
                    {
                        ;
                    }
                    return cpf;
                });

                foreach(var r in res)
                {
                    Console.WriteLine(r);
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}
